<?php
class M_level extends Nvl_Model{
	function __construct(){
		parent::__construct();
		$this->table_name='m_level';
		$this->column_primary='level_id';
		$this->column_order='';
		$this->column_unique='level_nama';
		$this->column_active='level_aktif';
	}
	
	function get_list($start, $end, $search_text){
		if($search_text != ''){
			$this->db->like('level_nama', $search_text);
		}
		if($start != 0 && $end != 0){
			$this->db->limit($end, $start);
		}
		$record = $this->db->get('m_level');
		$final[] = array(
			"total"=>$record->num_rows(),
			"results"=>$record->result()
		);
		return json_encode($final);
	}
	
	function get_list_skor($level_id){
		$level_id = $this->input->numeric('level_id','get');
		$this->db->select("
			skor_id,
			CASE WHEN skor_juara_id IS NULL THEN juara_id ELSE skor_juara_id END AS skor_juara_id,
			CASE WHEN skor_angka IS NULL THEN 0 ELSE skor_angka END AS skor_angka,
			juara_nama
		", FALSE);
		$this->db->order_by('juara_id');
		if($level_id != 0){
			$this->db->join('t_skor', 'juara_id = skor_juara_id AND skor_level_id ='.$level_id.' OR skor_level_id IS NULL', 'left');
		}else{
			$this->db->join('t_skor', 'juara_id = skor_juara_id AND skor_level_id IS NULL', 'left');
		}
		$query = $this->db->get('m_juara');
		$final[] = array(
			"total"=>$query->num_rows(),
			"results"=>$query->result()
		);
		return json_encode($final);
	}
}